//
//  homeTwoTableViewCell.m
//  fp
//
//  Created by xingshuai on 2020/11/14.
//

#import "homeTwoTableViewCell.h"

// cell
#import "HomeTeoCollectionViewCell.h"

static NSString *identifier = @"XAHoneTwoSectionCollectionCellID";

@interface homeTwoTableViewCell () <UICollectionViewDelegate, UICollectionViewDataSource>
// 随机学校
@property (nonatomic, strong) UIButton *schoolBtn;
// 随机学校名字
@property (nonatomic, strong) UILabel *schoolName;
// 数据展示
@property (nonatomic, strong) UICollectionView *collection;
// cell图片数组
@property (nonatomic, strong) NSArray *imgArr;

@end

@implementation homeTwoTableViewCell

+ (instancetype)cellWithTableView:(UITableView *)tableView  {
    static NSString *identifier = @"XAHomeTwoCellID";
    id cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[self alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
    }
    return cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.contentView.backgroundColor = [UIColor clearColor];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self creatHomeTwoCellUI];
    }
    return self;
}

-(NSArray *)imgArr {
    if (!_imgArr) {
        _imgArr = @[@"home_sound",@"home_game",@"home_life",@"home_zoo",@"home_local",@"home_mud"];
    }
    return _imgArr;
}

-(void)creatHomeTwoCellUI {
    self.schoolBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.schoolBtn setBackgroundImage:[UIImage imageNamed:@"bg-randSchool"] forState:UIControlStateNormal];
    [self.contentView addSubview:self.schoolBtn];
    [self.schoolBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.contentView);
        make.top.bottom.bottom.equalTo(self.contentView);
        make.width.mas_offset((ScreenWidth-30)/5.4);
    }];
    
    self.schoolName = [[UILabel alloc]init];
    self.schoolName.text = @"上海市外国语大学附属中小学";
    self.schoolName.numberOfLines = 2;
    self.schoolName.textAlignment = NSTextAlignmentCenter;
    self.schoolName.textColor = [UIColor colorWithHexString:@"#4E75FF"];
    self.schoolName.font = [UIFont pingFangSCWithWeight:FontWeightStyleRegular size:18];
    [self.schoolBtn addSubview:self.schoolName];
    [self.schoolName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.schoolBtn);
        make.left.equalTo(self.schoolBtn).offset(15);
        make.right.bottom.equalTo(self.schoolBtn).offset(-15);
    }];
    
    [self.contentView addSubview:self.collection];
    [self.collection mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.bottom.equalTo(self.contentView);
        make.right.equalTo(self.schoolBtn.mas_left).offset(-15);
    }];
}

// 初始化collection
- (UICollectionView *)collection {
    if (!_collection) {
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        flowLayout.minimumLineSpacing = 10;
        flowLayout.minimumInteritemSpacing = 10;
        flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
        
        _collection = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout];
        [_collection registerClass:[HomeTeoCollectionViewCell class] forCellWithReuseIdentifier:identifier];
        _collection.showsHorizontalScrollIndicator = NO;
        _collection.dataSource = self;
        _collection.delegate = self;
        _collection.scrollEnabled = NO;
        _collection.backgroundColor = [UIColor clearColor];
        _collection.showsVerticalScrollIndicator = NO;
    }
    return _collection;
}

#pragma mark - UICollectionViewDelegateFlowLayout
// 返回cell的尺寸大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    CGSize sizeOne = CGSizeMake((collectionView.frame.size.width-10)/2, self.schoolBtn.bounds.size.height/2-5);
    CGSize sizeTwo = CGSizeMake((collectionView.frame.size.width-30)/4, self.schoolBtn.bounds.size.height/2-5);
    return indexPath.row == 0 || indexPath.row == 1 ? sizeOne : sizeTwo;
}

#pragma mark - deleDate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.imgArr.count; //section == 0 ? 2 : 4;
}

// cell内容
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    HomeTeoCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
    cell.imgString = [self.imgArr objectAtIndex:indexPath.row];
    
    return cell;
}


- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
